<style scoped>
.wraper {
    padding: 0 30px;
}

.search {
    padding-bottom: 20px;
}

.search span {
    color: #68777c;
    font-weight: bold;
    padding-right: 18px;
}

.search button {
    background-color: #00a8f7;
    color: #fff;
    margin-left: 8px;
}

.top {
    margin-bottom: 20px;
    border-top: 1px dashed #cccccc;
}

.detail {
    border-top: 1px dashed #cccccc;
}

.wraper .headline {
    position: relative;
    font-size: 12px;
    color: #303134;
    font-weight: bold;
    margin: 10px 0;
    line-height: 32px;
    height: 32px;
}

.wraper .add-agent {
    position: absolute;
    top: 0px;
    right: 30px;
    background-color: #348bf2;
    color: #fff;
}

.iconfont {
    font-size: 14px;
}

.red-font {
    color: #ff1f1f;
}

/* 分页 start */

.page {
    margin: 20px 0;
}
.ivu-page {
    padding-bottom: 150px;
} 

/* 分页 end */


</style>
<style>
.delconfirm .ivu-modal .ivu-modal-header{
    background-color: transparent;
}
.delconfirm .ivu-modal .ivu-modal-close i{
    color: #999;
}
</style>


<template>
    <div class="employee-manage">
        <product-title></product-title>
        <div class="wraper">
            <div class="search">
                <span>快速搜索</span>
                    <Input v-model="searchForm.name" placeholder="请输入机构名称" style="width: 140px"></Input>
                    <Input v-model="searchForm.agency_code" placeholder="请输入机构代码" style="width: 140px"></Input>
                <Button @click='search'>查询</Button>
            </div>
            <div class="detail">
                <div class="headline">
                    <Button class="add-agent" @click='toDetail(-1)'>新增机构</Button>                   
                </div>
                <div class="clearfix">
                    <Table border :columns="columns_org" :data="data_org"></Table>
                    <Row class="page fr">
                        <Col span="24">
                            <Page :total="orgTotal" show-total show-sizer @on-change="pageChange" @on-page-size-change="pageSizeChange"></Page>
                        </Col>
                    </Row>
                </div>
            </div>
        </div>
        <Modal v-model="showAddModal" :mask-closable='closeable' width="600" title="添加/编辑账号信息" @on-ok="add" :loading="loading">
            <Row>
                <Col span="18" offset="1">
                    <Form :model="org" ref="org" :rules="ruleValidate" :label-width="120">
                        <Form-item label="账号名：" prop="name" required>
                            <Input v-model="org.name" placeholder="请输入账号登录名"></Input>
                        </Form-item>
                        <Form-item label="登录名：" prop="login_name" required>
                            <Input v-model="org.login_name" placeholder="请输入账号登录名"></Input>
                        </Form-item>
                        <Form-item label="登录密码：" prop="pwd" required>
                            <Input v-model="org.pwd" type="password" placeholder="请输入登录密码"></Input>
                        </Form-item>
                        <Form-item label="确认密码：" prop="pwd_two" required>
                            <Input v-model="org.pwd_two" type="password" placeholder="请再次输入登录密码"></Input>
                        </Form-item>
                        <Form-item label="角色：" prop="role_id" required>
                            <Select v-model="org.role_id" style="width:306px" placeholder='请选择角色'>
                                <Option :value="item.id" v-for="item in roleList" :key="item.id" >{{item.name}}</Option>
                            </Select>
                        </Form-item>
                        <Form-item label="限额：" prop="quota" required>
                            <Input v-model="org.quota" placeholder="请输入机构限额"></Input>
                        </Form-item>                        
                    </Form>
                </Col>
            </Row>
        </Modal>
        <Modal v-model="delconfirm" width="360" class="delconfirm">
            <p slot="header" style="color:#f60;text-align:center">
                <Icon type="information-circled"></Icon>
                <span>删除确认</span>
            </p>
            <div style="text-align:center">
                <p>此机构已设置子账号</p>
                <p>是否继续删除?</p>
            </div>
            <div slot="footer">
                <Button type="error" size="large" long  @click="delConfirm()">删除</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import productTitle from '../../components/productTitle'
import { getOrgList , getPermission , setPermission ,  getYun, orgDel} from '../../api/org'
import { getRoleList} from '../../api/role'
export default {
    data() {
        const validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.org.pwd_two !== '') {
                    // 对第二个密码框单独验证
                    this.$refs.org.validateField('pwd_two');
                }
                callback();
            }
        };
        const validatePassCheck = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.org.pwd) {
                callback(new Error('两次输入的密码不匹配!'));
            } else {
                callback();
            }
        };
        return {
            cou:-1,
            page: 1,
            pagesize: 10,
            showAddModal:false,
            closeable: false,
            loading: true,
            delconfirm:false,
            searchForm: {
                name: '',
            },
            orgList:[

            ],
            roleList:[],
            org:{
                name:'',
                login_name:'',
                pwd:'',
                role_id:'',
                quota:''
            },
            orgTotal: 0,
            columns_org: [
                {
                    title: '机构代码',
                    key: 'agency_code',
                    align: 'center',
                },
                {
                    title: '机构名称',
                    key: 'name',
                    align: 'center',
                },
                {
                    title: '机构电话',
                    key: 'tel',
                    align: 'center',
                },
                {
                    title:'负责人',
                    key:'principal',
                    align:'center'
                },
                {
                    title:'设立时间',
                    key:'create_date',
                    align:'center'
                },
                {
                    title:'状态',
                    key:'status',
                    align:'center'
                },
                {
                    title:'账号名',
                    key:'name',
                    align:'center'
                },
                {
                    title:'注册地',
                    key:'registe_addr',
                    align:'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    width: 200,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.openModal(params.row.id)
                                    }
                                }
                            }, '设置'),
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.toDetail(params.row.id)
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.remove(params.row.id)
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                },
            ],
            data_org: [

            ],
            ruleValidate: {
                name:[
                    { required: true, message: '帐号名不能为空', trigger: 'blur' }
                ],
                pwd: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                pwd_two: [
                    { validator: validatePassCheck, trigger: 'blur' }
                ],
                login_name: [
                    { required: true, message: '登录名不能为空', trigger: 'blur' },
                ],
                role_id: [
                    { required: true, message: '请选择角色', trigger: 'change' }
                ],
                quota:[
                    { required: true, message: '限额不能为空', trigger: 'blur' }
                ]
            },
            oid:'',
        }
    },
    components: {
        productTitle
    },
    created() {
        this.getList()
        this.getRole()
    },
    methods: {
        openModal(id){
            this.showAddModal = true
            this.org.oid = id
            var _param = {
                oid:id
            }
            new Promise((resolve, reject) => {
                getPermission(_param).then(response => {
                    if (response.data.err == 0) {
                        this.org =  response.data.data
                        this.org.pwd_two = response.data.data.pwd
                        this.org.oid =  id                      
                    } else {
                        this.$refs['org'].resetFields();
                        //this.$Message.error(response.data.info);
                        
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        add(){
            this.$refs['org'].validate((valid) => {
                if (valid) {                    
                    new Promise((resolve, reject) => {
                        setPermission(this.org).then(response => {
                            if (response.data.err == 0) {
                                this.showAddModal = false;
                                this.loading = true;
                                this.$Message.success(response.data.info)
                            } else {
                                this.$Message.error(response.data.info)
                                setTimeout(() => {
                                    this.loading = false;
                                    this.$nextTick(() => {
                                        this.loading = true;
                                    });
                                }, 2000);
                            }
                            resolve();
                        }).catch(error => {
                            reject(error);
                        });
                    });                                    
                } else {
                    // 通过loading来阻止modal隐藏，然后手动修改loading来充值确定按钮
                    setTimeout(() => {
                        this.loading = false;
                        this.$nextTick(() => {
                            this.loading = true;
                        });
                    }, 2000);
                }    
            })
        },

        //获取角色列表
        getRole() {
            let _params = {
                cate:2
            }
            new Promise((resolve, reject) => {
                getRoleList(_params).then(response => {
                    if (response.data.err == 0) {
                        this.roleList = response.data.data;
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //
        toDetail(id){
             this.$router.push({'name':'org_detail',params:{id:id}});
        },
        // 搜索
        search() {
            this.getList()           
        },
        getList(){  
            let _param = {
                p:this.page,
                pagesize:this.pagesize,
                name:this.searchForm.name,
                agency_code:this.searchForm.agency_code
            }          
            new Promise((resolve, reject) => {
                getOrgList(_param).then(response => {
                    console.log(response.data.data);
                    if (response.data.err == 0) {
                        this.data_org = response.data.data.list
                        this.orgTotal = response.data.data.total
                    }else{
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        // 页码改变
        pageChange(page) {
            this.page = page
            this.getList()
        },
        // 每页条数改变
        pageSizeChange(pagesize) {
            this.pagesize = pagesize
            this.getList();
        },
        remove(id){
            this.$Modal.confirm({
                title: '提示',
                content: '是否要删除该条记录?',
                onOk: () =>{
                    let _param = {
                        oid : id
                    }
                    new Promise((resolve, reject) => {
                        getYun(_param).then(response => {
                            if (response.data.err == 0) { 
                                if(response.data.data.has_yun ){
                                    this.oid = id
                                    this.delconfirm = true
                                    //this.delConfirm(id)                                   
                                }else{
                                    this.del(id)
                                }                                                             
                            } else {
                                this.$Message.error(response.data.info);
                            }
                            resolve();
                        }).catch(error => {
                            reject(error);
                        });
                    });
                },
            });            
        },
        delConfirm(){           
            let _param = {
                oid : this.oid
            }
            new Promise((resolve, reject) => {
                orgDel(_param).then(response => {
                    if (response.data.err == 0) { 
                        this.$Message.success(response.data.info);
                        this.delconfirm = false 
                        this.getList()                                                               
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });                        
        },
        del(id){
            let _param = {
                oid : id
            }
            new Promise((resolve, reject) => {
                orgDel(_param).then(response => {
                    if (response.data.err == 0) { 
                        this.$Message.success(response.data.info); 
                        this.getList()                                                               
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        }

    }
}
</script>
